<template>
  <div>
    <el-row
      type="flex"
      justify="space-around"
      align="middle"
      :gutter="20"
      style="margin-bottom: 10px"
    >
      <el-col :span="1">状态：</el-col>
      <el-col :span="1">
        <el-switch
          v-model="isOnline"
          active-color="#39BC65"
          inactive-color="#ff4949"
          :active-text="showName"
        ></el-switch>
      </el-col>
      <el-col :span="2">选择线别：</el-col>
      <el-col :span="3" :pull="1">
        <el-select v-model="lineType" placeholder="请选择">
          <el-option
            v-for="item in lineTypeOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-col>
      <el-col :span="2">所属单位：</el-col>
      <el-col :span="3" :pull="1">
        <el-select v-model="orgId" placeholder="请选择">
          <el-option
            v-for="item in orgOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-col>
      <el-col :span="2">定位标签：</el-col>
      <el-col :span="3" :pull="1">
        <el-input
          v-model="locationLabel"
          placeholder="请输入定位标签"
        ></el-input>
      </el-col>
      <el-col :span="2">用户姓名：</el-col>
      <el-col :span="3" :pull="1">
        <el-input v-model="userName" placeholder="请输入用户姓名"></el-input>
      </el-col>
      <el-col :span="3">
        <el-button type="primary" @click="searchHandler">搜索</el-button>
        <el-button type="info" @click="resetHandler">重置</el-button>
      </el-col>
    </el-row>
    <el-row>
      <el-table
        :data="renderData"
        border
        style="width: 100%"
        :header-cell-style="{ background: '#808080', color: '#fff' }"
      >
        <el-table-column
          type="index"
          label="序号"
          width="80"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="lineType"
          label="线别"
          width="180"
          align="center"
        >
        </el-table-column>
        <el-table-column prop="orgName" label="所属单位" align="center">
        </el-table-column>
        <el-table-column prop="userName" label="用户姓名" align="center">
        </el-table-column>
        <el-table-column prop="userLabel" label="用户标签" align="center">
        </el-table-column>
        <el-table-column prop="isOnline" label="状态" align="center">
          <template slot-scope="scope">
            <span>
              {{ scope.row.isOnline === 1 ? "在线" : "离线" }}
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="locationKm" label="用户位置" align="center">
          <template slot-scope="scope">
            DK{{ scope.row.locationKm }}+{{ scope.row.locationM }}
          </template>
        </el-table-column>
        <el-table-column prop="entryTime" label="入场时间" align="center">
        </el-table-column>
        <el-table-column prop="exitTime" label="出场时间" align="center">
        </el-table-column>
        <el-table-column prop="updateTime" label="更新时间" align="center">
        </el-table-column>
      </el-table>
    </el-row>
    <el-row style="position: absolute; right: 50px">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[10, 20, 50]"
        :page-size="10"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-row>
  </div>
</template>

<script>
import { getUserLocation } from "/src/http/productSchedule/personnelPosit.js";
import { getDataType } from "/src/http/productSchedule/dictionary.js";
import { getTreeList } from "/src/http/productSchedule/companyMg.js";
export default {
  name: "lineInspect",
  data() {
    return {
      lineType: "",
      orgId: null,
      locationLabel: "",
      userName: "",
      isOnline: true,
      lineTypeOptions: [
        {
          value: "左线",
          label: "左线",
        },
        {
          value: "右线",
          label: "右线",
        },
      ],
      orgOptions: [],
      total: 0,
      pageSize: 10,
      currentPage: 1,
      isSearch: false,
      renderData: [],
    };
  },
  watch: {
    isOnline: function (newVal) {
      console.log("newVal::: ", newVal);
      this.isOnline = newVal;
      this.getUserLocationApi(1, this.pageSize, this.isOnline === true ? 1 : 0);
    },
  },
  computed: {
    showName() {
      return this.isOnline === true ? "在线" : "离线";
    },
  },
  mounted() {
    this.getUserLocationApi();
    this.getTreeListApi();
    this.getDataTypeApi();
  },

  methods: {
    searchHandler() {
      this.isSearch = true;
      if (
        (this.lineType === "") &
        (this.orgId === null) &
        (this.locationLabel === "") &
        (this.userName === "")
      ) {
        this.$message({
          message: "请输入查询条件",
          type: "warning",
        });
      } else {
        this.getUserLocationApi(
          1,
          this.pageSize,
          this.isOnline === true ? 1 : 0,
          this.lineType,
          this.orgId,
          this.locationLabel,
          this.userName
        );
      }
    },

    resetHandler() {
      this.getUserLocationApi();
      this.isSearch = false;
      this.isOnline = true;
      this.lineType = "";
      this.orgId = null;
      this.locationLabel = "";
      this.userName = "";
    },

    handleSizeChange(v) {
      this.pageSize = v;
      if (this.isSearch === true) {
        this.getUserLocationApi(
          1,
          v,
          this.isOnline === true ? 1 : 0,
          this.lineType,
          this.orgId,
          this.locationLabel,
          this.userName
        );
      } else {
        this.getUserLocationApi(1, v, this.isOnline === true ? 1 : 0);
      }
    },

    handleCurrentChange(v) {
      this.currentPage = v;
      if (this.isSearch === true) {
        this.getUserLocationApi(
          v,
          this.pageSize,
          this.isOnline === true ? 1 : 0,
          this.lineType,
          this.orgId,
          this.locationLabel,
          this.userName
        );
      }
      this.getUserLocationApi(v, this.pageSize, this.isOnline === true ? 1 : 0);
    },

    deleteHandler(v) {
      this.$confirm("是否删除该条报警信息？", "提示", {
        confirmButtonText: "确定删除",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.renderData = this.renderData.filter((el) => {
            return el.id !== v.id;
          });
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    getTreeListApi() {
      getTreeList().then((res) => {
        if (res.code == 200) {
          this.orgOptions = res.data;
        }
      });
    },

    getDataTypeApi() {
      getDataType({ type: "ht_line_type" }).then((res) => {
        if (res.code == 200) {
          this.lineTypeOptions = res.data;
        }
      });
    },

    getUserLocationApi(
      pageNum = 1,
      pageSize = 10,
      isOnline = 1,
      lineType = null,
      orgId = null,
      locationLabel = "",
      userName = ""
    ) {
      let params = {
        pageNum,
        pageSize,
        isOnline,
        lineType,
        orgId,
        locationLabel,
        userName,
      };
      getUserLocation(params).then((res) => {
        if (res.code == 200) {
          this.renderData = res.data.records;
          this.total = res.data.total;
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.el-date-editor {
  margin: 0 10px;
}

:deep(.el-switch) {
  position: relative;
  user-select: none;

  .el-switch__core {
    width: 60px !important;
  }

  &.is-checked .el-switch__label {
    position: absolute;
    top: 0px;
    left: -3px;
    color: #fff;
  }
  .el-switch__label {
    position: absolute;
    top: 0px;
    left: 10px;
    color: #fff;
  }
}
</style>
